package com.bw;

import com.hierynomus.msfscc.fileinformation.FileIdBothDirectoryInformation;
import com.hierynomus.smbj.SMBClient;
import com.hierynomus.smbj.auth.AuthenticationContext;
import com.hierynomus.smbj.connection.Connection;
import com.hierynomus.smbj.session.Session;
import com.hierynomus.smbj.share.DiskShare;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class CheckNAS3 {

    // 支持的录屏文件扩展名
    private static final Set<String> SCREEN_RECORD_EXTENSIONS = new HashSet<>(
            Arrays.asList(".mp4", ".avi", ".mov", ".mkv", ".wmv")
    );


    public static void main(String[] args) throws IOException {




        String server = "10.160.60.60";          // SMB服务器地址
        String shareName = "八维教育天津校区";            // 共享名称
        String username = "hucongcong";
        String password = "666666";

        ArrayList<String> stuNameList = new ArrayList<>();
        ArrayList<String> errList = new ArrayList<>();


//        String rootPath = "北京大数据院\\" + str;            // 根目录路径，例如班级名称
        String rootPath = "北京大数据院\\大数据2212A\\专高一\\日考\\3.4";            // 根目录路径，例如班级名称

        SMBClient client = new SMBClient();
        try (Connection conn = client.connect(server)) {

            AuthenticationContext auth = new AuthenticationContext(
                    username, password.toCharArray(), null); // 域设为null若无需
            Session session = conn.authenticate(auth);

            try (DiskShare share = (DiskShare) session.connectShare(shareName)) {
                // 遍历所有学生目录（二级目录）
                for (FileIdBothDirectoryInformation studentDir : share.list(rootPath)) {
                    String studentName = studentDir.getFileName();
                    boolean isDirectory = (studentDir.getFileAttributes() & 0x10) != 0;
                    if (!isDirectory || skipSpecialDir(studentName)) continue;

                    stuNameList.add(studentName);
                    String studentPath = rootPath + "\\" + studentName;
                    int screenCount = countScreenRecords(share, studentPath);

                    if (screenCount < 2) {
                        errList.add(studentName);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("!!!!!!请确认目录是否存在!!!!!!!");
                return;
            }
        }
        System.out.println("***************扫描到以下【" + stuNameList.size() + "】个目录***************");

        for (String s : stuNameList) {
            System.out.println(s);
        }

        if (errList.size() == 0) {
            System.out.println("***************未检测到缺失录屏的目录***************");
            return;
        }

        System.out.println("***************检测到以下【 " + errList.size() + "】个录屏不全的目录***************");

        for (String s : errList) {
            System.out.println(s);
        }
        System.out.println("***************结束***************");


    }

    /**
     * 统计指定路径下所有文件及其直接子目录中的录屏文件数量
     */
    private static int countScreenRecords(DiskShare share, String basePath) {
        int total = 0;
        // 统计当前目录文件
        total += countCurrentDirFiles(share, basePath);
        // 统计直接子目录中的文件
        total += countSubdirFiles(share, basePath);
        return total;
    }

    /**
     * 统计当前目录下的录屏文件
     */
    private static int countCurrentDirFiles(DiskShare share, String path) {
        int count = 0;
        for (FileIdBothDirectoryInformation file : share.list(path)) {
            boolean isDirectory = (file.getFileAttributes() & 0x10) != 0;
            if (!isDirectory && isScreenRecord(file.getFileName())) {
                count++;
            }
        }
        return count;
    }

    /**
     * 统计所有直接子目录中的录屏文件
     */
    private static int countSubdirFiles(DiskShare share, String basePath) {
        int count = 0;
        for (FileIdBothDirectoryInformation dir : share.list(basePath)) {
            String dirName = dir.getFileName();
            boolean isDirectory = (dir.getFileAttributes() & 0x10) != 0;
            if (isDirectory && !skipSpecialDir(dirName)) {
                String subPath = basePath + "\\" + dirName;
                count += countCurrentDirFiles(share, subPath);
            }
        }
        return count;
    }

    /**
     * 判断文件名是否为录屏文件
     */
    private static boolean isScreenRecord(String fileName) {
        String ext = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();
        return SCREEN_RECORD_EXTENSIONS.contains(ext);
    }

    /**
     * 跳过特殊目录
     */
    private static boolean skipSpecialDir(String dirName) {
        return dirName.startsWith(".") || dirName.startsWith(".") || dirName.equals(".") || dirName.equals("..");
    }
}
